package cn.iocoder.yudao.module.eval.controller.admin.filingmanagement;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.eval.controller.admin.filingmanagement.vo.*;
import cn.iocoder.yudao.module.eval.dal.dataobject.filingmanagement.FilingManagementDO;
import cn.iocoder.yudao.module.eval.service.filingmanagement.FilingManagementService;

@Tag(name = "管理后台 - 备案管理")
@RestController
@RequestMapping("/eval/filing-management")
@Validated
public class FilingManagementController {

    @Resource
    private FilingManagementService filingManagementService;

    @PostMapping("/create")
    @Operation(summary = "创建备案管理")
    @PreAuthorize("@ss.hasPermission('eval:filing-management:create')")
    public CommonResult<Long> createFilingManagement(@Valid @RequestBody FilingManagementSaveReqVO createReqVO) {
        return success(filingManagementService.createFilingManagement(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新备案管理")
    @PreAuthorize("@ss.hasPermission('eval:filing-management:update')")
    public CommonResult<Boolean> updateFilingManagement(@Valid @RequestBody FilingManagementSaveReqVO updateReqVO) {
        filingManagementService.updateFilingManagement(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除备案管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:filing-management:delete')")
    public CommonResult<Boolean> deleteFilingManagement(@RequestParam("id") Long id) {
        filingManagementService.deleteFilingManagement(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除备案管理")
                @PreAuthorize("@ss.hasPermission('eval:filing-management:delete')")
    public CommonResult<Boolean> deleteFilingManagementList(@RequestParam("ids") List<Long> ids) {
        filingManagementService.deleteFilingManagementListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得备案管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('eval:filing-management:query')")
    public CommonResult<FilingManagementRespVO> getFilingManagement(@RequestParam("id") Long id) {
        FilingManagementDO filingManagement = filingManagementService.getFilingManagement(id);
        return success(BeanUtils.toBean(filingManagement, FilingManagementRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得备案管理分页")
    @PreAuthorize("@ss.hasPermission('eval:filing-management:query')")
    public CommonResult<PageResult<FilingManagementRespVO>> getFilingManagementPage(@Valid FilingManagementPageReqVO pageReqVO) {
        PageResult<FilingManagementDO> pageResult = filingManagementService.getFilingManagementPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FilingManagementRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出备案管理 Excel")
    @PreAuthorize("@ss.hasPermission('eval:filing-management:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFilingManagementExcel(@Valid FilingManagementPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FilingManagementDO> list = filingManagementService.getFilingManagementPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "备案管理.xls", "数据", FilingManagementRespVO.class,
                        BeanUtils.toBean(list, FilingManagementRespVO.class));
    }

}